//读取XML文件
package com.security.lasys.agent.plugin;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Vector;

/**
 * 读取xml文件格式的类
 */
public class ConfigPraser {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private SAXReader saxReader;//读取xml中的数据，并且返回document格式的对象

    private XMLWriter output;//写xml

    private String filename;

    private Document doc;

    public ConfigPraser(String filename){
        this.filename = filename;
        saxReader = new SAXReader();
        try{
            doc = saxReader.read(filename);
        }catch (DocumentException e){
            logger.error("读取" + filename + "失败");
            e.printStackTrace();
        }
    }

    //获取整个xml文件当中的titles
    //insatnceof 用来确认该特定类是否是其子类的一个实例
    public Vector<String> showTitles(){
        Vector<String> result = new Vector<>();
        Element root = doc.getRootElement();
        for (int i = 0 ; i <root.nodeCount() ; i++){
            Node node = root.node(i);
            if (node instanceof Element){
                Element e = (Element) node;
                result.add(e.getName());
            }
        }
        return result;
    }

    //展示一个title的name
    public Vector<String> showNames(String title){
        Vector<String> result = new Vector<>();
        Element root = doc.getRootElement();
        for (int i = 0 ; i < root.nodeCount() ; i++){
            Node node = root.node(i);
            if (node instanceof  Element){
                Element e = (Element) node;
                if (e.getName().equals(title)){
                    for(int k = 0 ; k < e.nodeCount() ; k++){
                        Node nodeName = e.node(k);
                        if (nodeName instanceof Element){
                            Element elementName = (Element) nodeName;
                            result.add(elementName.getName());
                        }
                    }
                }
            }
        }
        return result;
    }

    public void add(String title,String name,String value){
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GBK");
        try{
            output = new XMLWriter(new FileWriter(new File(filename)),format);

            Element root = doc.getRootElement();
            for (int i = 0 ; i < root.nodeCount() ; i++){
                Node node = root.node(i);
                if (node instanceof Element){
                    Element e = (Element) node;
                    if (e.getName().equals(title)){
                        Element username = e.addElement(name);
                        username.setText(value);
                    }
                }
            }
            output.write(doc);
            output.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public void removeSection(String section){
        OutputFormat format = OutputFormat.createPrettyPrint();

        format.setEncoding("GBK");
        try{
            output = new XMLWriter(new FileWriter(new File(filename)),format);
            Element root = doc.getRootElement();

            for (int i = 0 ; i < root.nodeCount() ; i++){
                Node node = root.node(i);
                if (node instanceof Element){
                    Element e = (Element) node;
                    if (e.getName().equals(section)){
                        logger.info(e.getName());
                        for (int j = 0 ; j < e.nodeCount(); j++){
                            Node node1 = e.node(j);
                            if (node1 instanceof  Element){
                                Element element = (Element) node1;
                                logger.info(element.getName());
                                e.remove(element);
                            }
                        }
                    }
                }
            }
            output.write(doc);
            output.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public Boolean set(String title,String name,String value){
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("GBK");
        Boolean flag = false;
        try{
            output = new XMLWriter(new FileWriter(new File(filename)),format);
            Element root = doc.getRootElement();
            for (int i = 0 ; i < root.nodeCount() ; i++){
                Node node = root.node(i);
                if (node instanceof Element){
                    Element e = (Element) node;
                    if (e.getName().equals(title)) {
                        // System.out.println(e.getName());
                        for (int k = 0; k < e.nodeCount(); k++) {
                            Node node1 = e.node(k);
                            if (node1 instanceof Element) {
                                Element e1 = (Element) node1;

                                if (e1.getName().equals(name)) {
                                    e1.setText(value);
                                    // value=e1.getStringValue();
                                    flag = true;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return flag;
    }

    public String read(String title,String name){
        String value = "no result";

        Element root = doc.getRootElement();
        for (int  i = 0 ; i < root.nodeCount() ; i++){
            Node node = root.node(i);
            if (node instanceof Element){
                Element e = (Element) node;
                if (e.getName().equals(title)){
                    for (int k = 0 ; k < e.nodeCount() ; k++){
                        Node node1 = e.node(k);
                        if (node1 instanceof Element){
                            Element element = (Element) node1;
                            if (element.getName().equals(name)){
                                value = element.getStringValue();
                                break;
                            }else{
                                value = "no result";
                            }
                        }
                    }
                    break;
                }else{
                    value = "no result";
                }
            }
        }
        return value;
    }

    public static void main(String[] args) {
        ConfigPraser configPraser = new ConfigPraser("./config/webserver.xml");
        for (String title:configPraser.showTitles()){
            Vector<String> names = configPraser.showNames(title);
            for (String name:names){
                System.out.print(name + " ");
                String value = configPraser.read(title,name);
                System.out.println(value);
            }
        }
    }
}
